/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVQL;

import DAO.CTHoaDonDao;
import GUI.TOOL.ExcelOpener;
import DAO.HoaDonDao;
import DAO.KhachHangDao;
import DAO.NhanVienDao;
import DAO.SanPhamDao;
import GUI.TOOL.XuatExcel;
import GUI.Var;
import POJO.CTHoaDon;
import POJO.HoaDon;
import POJO.KhachHang;
import POJO.NhanVien;
import POJO.SanPham;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Khoi
 */
public class frXemDanhSachHoaDon extends javax.swing.JInternalFrame {

	/**
	 * Creates new form frXemDanhSachHoaDon
	 */
	DefaultTableModel modelHD;
	DefaultTableModel modelCT;
	private int TSD;// tong so dong trong bang hoa don
	private int THT; //trang hiện tại
	private int SD;// hien thi bao nhieu dong tron trang

	private void defaultHoaDon() {
		String[] columns = new String[]{"STT", "Số HD", "Ngày lập", "Nhân viên phụ trách", "Khách hàng", "Số tiền giảm", "Trị giá"};
		modelHD = new DefaultTableModel(null, columns);

	}

	public frXemDanhSachHoaDon() {
		initComponents();
		TSD = soDongDuLieu();
		THT = 0;
		SD = 20;

		load(SD, THT);
		if (modelHD.getRowCount() > 0) {
			//tbHoaDonSelectedRow() =0;
			loadChiTiet(0);
		}
		setTxt();
	}

	private int soDongDuLieu() {
		int tmp = 0;
		HoaDonDao dao = new HoaDonDao();
		try {
			tmp = dao.laySoDong();
		} catch (Exception e) {
		}
		return tmp;
	}

	public void defaultChiTiet() {
		String[] columns = new String[]{"STT", "Sản phẩm", "Số lượng", "Giá", "Tổng tiền"};
		modelCT = new DefaultTableModel(null, columns);
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              jPanel4 = new javax.swing.JPanel();
              jScrollPane2 = new javax.swing.JScrollPane();
              tbHoaDon = new javax.swing.JTable();
              jPanel6 = new javax.swing.JPanel();
              jButton3 = new javax.swing.JButton();
              jButton1 = new javax.swing.JButton();
              jButton5 = new javax.swing.JButton();
              jButton4 = new javax.swing.JButton();
              lbPhanTrang = new javax.swing.JLabel();
              jPanel5 = new javax.swing.JPanel();
              jPanel2 = new javax.swing.JPanel();
              jScrollPane3 = new javax.swing.JScrollPane();
              tbChiTiet = new javax.swing.JTable();
              jPanel3 = new javax.swing.JPanel();
              jbtnLapBaoCao = new javax.swing.JButton();
              jButton2 = new javax.swing.JButton();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel1.setText("XEM DANH SÁCH HÓA ĐƠN");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách hóa đơn"));
              jPanel4.setToolTipText("");

              tbHoaDon.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Số hóa đơn", "Ngày lập", "Nhân viên phụ trách", "Khách hàng", "Số tiền giảm", "Trị giá"
                     }
              ));
              tbHoaDon.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            tbHoaDonMouseClicked(evt);
                     }
              });
              jScrollPane2.setViewportView(tbHoaDon);

              jButton3.setText("|<");
              jButton3.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton3ActionPerformed(evt);
                     }
              });

              jButton1.setText("<");
              jButton1.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton1ActionPerformed(evt);
                     }
              });

              jButton5.setText(">|");
              jButton5.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton5ActionPerformed(evt);
                     }
              });

              jButton4.setText(">");
              jButton4.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton4ActionPerformed(evt);
                     }
              });

              lbPhanTrang.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              lbPhanTrang.setText("0");

              javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
              jPanel6.setLayout(jPanel6Layout);
              jPanel6Layout.setHorizontalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel6Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lbPhanTrang)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel6Layout.setVerticalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jButton3)
                                   .addComponent(jButton1)
                                   .addComponent(jButton5)
                                   .addComponent(jButton4)
                                   .addComponent(lbPhanTrang)))
              );

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane2)
                     .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
              );

              jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tiết hóa đơn"));

              tbChiTiet.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Sản phẩm", "Số lượng", "Giá", "Tổng tiền"
                     }
              ));
              jScrollPane3.setViewportView(tbChiTiet);

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGap(0, 499, Short.MAX_VALUE)
                     .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE))
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGap(0, 0, Short.MAX_VALUE)
                     .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))
              );

              jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Chức năng"));

              jbtnLapBaoCao.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jbtnLapBaoCao.setText("Lập báo cáo");
              jbtnLapBaoCao.setToolTipText("gọi lập phiếu nhập kho");
              jbtnLapBaoCao.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnLapBaoCaoActionPerformed(evt);
                     }
              });

              jButton2.setText("Lưu ra Excel");
              jButton2.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton2ActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(jbtnLapBaoCao, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                                   .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, Short.MAX_VALUE))
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                            .addComponent(jbtnLapBaoCao)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2)
                            .addContainerGap(99, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
              jPanel5.setLayout(jPanel5Layout);
              jPanel5Layout.setHorizontalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
              );
              jPanel5Layout.setVerticalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

	private void load(int soDong, int trangSo) {
		HoaDonDao dao = new HoaDonDao();
		ArrayList<HoaDon> dshd = new ArrayList<HoaDon>();
		try {
			dshd = dao.layDanhSach(soDong, trangSo);
		} catch (Exception e) {
		}
		NhanVienDao nvd = new NhanVienDao();
		KhachHangDao khd = new KhachHangDao();

		NhanVien nv = new NhanVien();
		KhachHang kh = new KhachHang();


		defaultHoaDon();
		int i = 1;
		for (HoaDon hd : dshd) {
			nv = nvd.layByMa(hd.getMaNVPhuTrach());
			kh = khd.layByMa(hd.getMaKH());
			Object[] items = new Object[]{
				i,
				hd.getMaHD(),
				Var.hienThiNgayVN(hd.getNgayLap()),
				nv.getTenNV(),
				kh.getTenKH(),
				hd.getSoTienGiam(),
				hd.getTriGia()
			};
			i++;
			//int maLSP = sp.getMaLSP();
			modelHD.addRow(items);
		}
		tbHoaDon.setModel(modelHD);
		// lam can doi cac cot
		int[] va = {5, 10, 15, 25, 25, 15, 15};
		int[] pt = new int[7];
		for (int l = 0; l < 7; l++) {
			pt[l] = tbHoaDon.getWidth() * va[l] / 100;
			tbHoaDon.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}

		// load Chi tiet hoa don
		if (modelHD.getRowCount() > 0) {
			loadChiTiet(0);
		}
	}

	private void setTxt() {
		lbPhanTrang.setText(Integer.toString(THT + 1) + "/" + Integer.toString(TSD / SD + 1));
	}
       private void jbtnLapBaoCaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLapBaoCaoActionPerformed
		load(SD, THT);
		if (modelHD.getRowCount() > 0) {
			loadChiTiet(0);
		}
		setTxt();
       }//GEN-LAST:event_jbtnLapBaoCaoActionPerformed
	private void loadChiTiet(int dong) {
		int maHd = (Integer) tbHoaDon.getValueAt(dong, 1);

		CTHoaDonDao dao = new CTHoaDonDao();
		ArrayList<CTHoaDon> dsct = new ArrayList<CTHoaDon>();
		try {
			dsct = dao.layDSByMaHD(maHd);
		} catch (Exception e) {
		}
		SanPhamDao spd = new SanPhamDao();
		SanPham sp = new SanPham();
		defaultChiTiet();
		int i = 1;
		for (CTHoaDon hd : dsct) {
			sp = spd.LaySanPhamByMa(hd.getMaSP());
			Object[] items = new Object[]{
				i,
				sp.getTenSP(),
				hd.getSoLuong(),
				hd.getGiaBan(),
				(Integer) hd.getSoLuong() * hd.getGiaBan()
			};
			i++;
			//int maLSP = sp.getMaLSP();
			modelCT.addRow(items);
		}
		tbChiTiet.setModel(modelCT);
		int[] va = {10, 30, 10, 20, 30};
		int[] pt = new int[5];
		for (int l = 0; l < 5; l++) {
			pt[l] = tbChiTiet.getWidth() * va[l] / 100;
			tbChiTiet.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
	}
       private void tbHoaDonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbHoaDonMouseClicked
		// TODO add your handling code here:
		int dong = tbHoaDon.getSelectedRow();
		if (dong != -1 && dong < modelHD.getRowCount()) {
			loadChiTiet(dong);
		}

       }//GEN-LAST:event_tbHoaDonMouseClicked
       private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
		// TODO add your handling code here:
		// ve dau tien
		THT = 0;
		load(SD, THT);
		setTxt();

       }//GEN-LAST:event_jButton3ActionPerformed

       private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
		// TODO add your handling code here:
		// ve cuoi

		THT = TSD / SD;
		load(SD, THT);

		setTxt();

       }//GEN-LAST:event_jButton5ActionPerformed

       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
		//ve trang truoc
		if (THT != 0) {
			THT -= 1;
			load(SD, THT);
		}
		setTxt();

       }//GEN-LAST:event_jButton1ActionPerformed

       private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
		// TODO add your handling code here:
		if (THT < TSD / SD) {
			THT += 1;
			load(SD, THT);
		}
		setTxt();

       }//GEN-LAST:event_jButton4ActionPerformed

       private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
		// TODO add your handling code here:
		try {
//     Note that i'm actually saving the file first
			XuatExcel exp = new XuatExcel();
			File file = new File("BaoCaoBanHangTrongThang.xls");
			exp.exportTable(tbHoaDon, file);
			ExcelOpener opn = new ExcelOpener();
			opn.openTable(file);
		} catch (IOException ex) {
			System.out.println(ex.getMessage());
			ex.printStackTrace();
		}
       }//GEN-LAST:event_jButton2ActionPerformed
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton jButton1;
       private javax.swing.JButton jButton2;
       private javax.swing.JButton jButton3;
       private javax.swing.JButton jButton4;
       private javax.swing.JButton jButton5;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JPanel jPanel5;
       private javax.swing.JPanel jPanel6;
       private javax.swing.JScrollPane jScrollPane2;
       private javax.swing.JScrollPane jScrollPane3;
       private javax.swing.JButton jbtnLapBaoCao;
       private javax.swing.JLabel lbPhanTrang;
       private javax.swing.JTable tbChiTiet;
       private javax.swing.JTable tbHoaDon;
       // End of variables declaration//GEN-END:variables
}
